/*
 * (C) Copyright 2005 Davide Brugali, Marco Torchiano
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 * 02111-1307  USA
 */
package multiformat;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

/**
 * The multiformat calculator.
 * Deze klasse wordt beschouwd als de model in de MVC.
 */
public class Calculator {
  private Rational operand_0 = new Rational();
  private Rational operand_1 = new Rational();
  
  // The current format of the calculator
  private Format format = new FixedPointFormat();
  // The current numberbase of the calculator
  private Base base = new DecimalBase();
  
  private ArrayList<ActionListener> actionListenerList = new ArrayList<ActionListener>();

  
  /*
   * @author Yiming Yung
   * Changed this method. Everytime this method is called. The input will be checked.
   * For every Base and rational format, checks are in place. If the input is incorrect a exception will be yielded.
   */
  public void addOperand(String newOperand) throws FormatException, NumberBaseException {
	  String digits = getBase().getDigits();
		  if(getBase().getName() == "dec"){
			  for(int i = 0; i < newOperand.length(); i++){
				  if(digits.indexOf(newOperand.charAt(i)) == -1 && newOperand.charAt(i) != '/'){
					  throw new NumberBaseException("only numbers");
				  }
			  }
			  processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));	
		  } else if(getBase().getName() == "hex"){
			  for(int i = 0; i < newOperand.length(); i++){
				  if(digits.indexOf(newOperand.charAt(i)) == -1 && newOperand.charAt(i) != '/'){
					  throw new NumberBaseException("only numbers and letters from A-F");
				  }
			  }
			  processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
		  } else if(getBase().getName() == "oct"){
			  for(int i = 0; i < newOperand.length(); i++){
				  if(digits.indexOf(newOperand.charAt(i)) == -1 && newOperand.charAt(i) != '/'){
					  throw new NumberBaseException("only numbers from 0-7");
				  }
			  }
			  processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
		  } else if(getBase().getName() == "bin"){
			  for(int i = 0; i < newOperand.length(); i++){
				  if(digits.indexOf(newOperand.charAt(i)) == -1 && newOperand.charAt(i) != '/'){
					  throw new NumberBaseException("only 1's and 0's");
				  }
			  }
			  processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
		  }
		  operand_1 = operand_0;
	      operand_0 = format.parse(newOperand, base);
  }

  public void add(){
    operand_0 = operand_1.plus(operand_0);
    operand_1 = new Rational();
    processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }
  public void subtract() {
    operand_0 = operand_1.minus(operand_0);
    operand_1 = new Rational();
    processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }
  public void multiply() {
    operand_0 = operand_1.mul(operand_0);
    operand_1 = new Rational();
    processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }
  public void divide() {
    operand_0 = operand_1.div(operand_0);
    operand_1 = new Rational();
    processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }
  public void delete() {
    operand_0 = operand_1;
    operand_1 = new Rational();
    processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }

  public String firstOperand(){
    return format.toString(operand_1,base);
    //processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }
  public String secondOperand(){
    return format.toString(operand_0,base);
  }

  public void setBase(Base newBase){
    base = newBase;
    processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }
  public Base getBase(){
    return base;
  }
  public void setFormat(Format newFormat){
    format = newFormat;
    processEvent( new ActionEvent( this, ActionEvent.ACTION_PERFORMED, null));
  }
  public Format getFormat(){
    return format;
  }
  public void addActionListener( ActionListener l){
	  actionListenerList.add( l );
  }	

  public void removeActionListener( ActionListener l){
	if ( actionListenerList.contains( l ) )
		actionListenerList.remove( l );
	}
	
	private void processEvent(ActionEvent e){
		// Hieronder gebruiken we het nieuwe Java "foreach" statement. 
		// Lees het als: "for each ActionListener in actionListenerList do ..."
		// Je kunt ook een for-lus of een iterator gebruiken, maar foreach is het elegantste.
		for( ActionListener l : actionListenerList)
			l.actionPerformed( e );
	}
}